﻿using ProjectCloth.Entity;
using ProjectCloth.FrmUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Export
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Catalog = "";
            Catalog = "clothdb0402";
            //Catalog = "clothdb0402"; 
            ClothContext.connection = $"Data Source=212.64.54.64;port=3306;Initial Catalog={Catalog};uid=root;password=root@cloth;Charset=utf8";
            ClothContext db = new ClothContext();
            var product = db.Products.OrderBy(o=>o.ProductNo).ToList();
            var list = (from a in db.InStockItems
                        join b in db.SaleOrderItems on a.SaleOrderItemId equals b.Id
                        join c in db.Products on b.ProductId equals c.Id
                        select new ProductInstock()
                        {
                            ProductNo = c.ProductNo,
                            Price = a.Price,
                            Productor = a.CompanyNo,
                            ProductCode = a.InstockProductName,
                            Unit=a.UnitStr
                        }).ToList();
            var listdata = new List<ProductInstock>();
            //foreach (var p in list.OrderBy(o => o.Price))
            //{
            //    var data = listdata.Where(o => o.ProductNo == p.ProductNo).OrderBy(o => o.Price).FirstOrDefault();
            //    if (data == null) listdata.Add(p);
            //    else
            //    if (data.Price > p.Price)
            //    { data.Price = p.Price; data.ProductCode = p.ProductCode; data.Productor = p.Productor; }
            //};
            foreach(var p in product)
            {
                var data = list.Where(o => o.ProductNo == p.ProductNo).OrderBy(o => o.Price).FirstOrDefault();
                if (data == null) listdata.Add(new ProductInstock
                {
                    ProductNo = p.ProductNo,
                    Price = null,

                });
                else listdata.Add(data);
            }
            dataGridView1.DataSource = listdata.OrderBy(o=>o.ProductNo).ToList();
        }
        public class ProductInstock
        {
            public string ProductNo { get; set; }
            public decimal? Price { get; set; }
            public string Productor { get; set; }
            public string ProductCode { get; set; }
            public string Unit { get; set; }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Helper.ExportDgv(dataGridView1, "商品-进货商品-信息");
        }
    }
}
